package cn.yunhe.service.impl;

import cn.yunhe.dao.PersonMapper;
import cn.yunhe.model.Page;
import cn.yunhe.model.Person;
import cn.yunhe.model.Union;
import cn.yunhe.service.IPersonService;
import cn.yunhe.view.HttpUtil;
import cn.yunhe.view.ReadExcel;
import com.alibaba.druid.sql.visitor.functions.If;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author Created by zs on 2017/12/20.
 */
@Service("personServiceImpl")
public class PersonServiceImpl implements IPersonService {


    public static final String POSTURL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";

    @Resource
    private PersonMapper personMapper;

    @Override
    public Page queryLikePersons(Map<String, Object> cond) {
        Page page = new Page();
        //总记录数
        page.setTotalNum(personMapper.getLikePersonsCount(cond));
        //每页大小
        int pageSize = Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        //当前页码
        int curPageNum = Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurPageNum(curPageNum);
        //总页数
        page.setTotalPage(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
        page.setResult(personMapper.getLikePersons(cond));
        return page;
    }

    /**
     * 导出表报
     * @return
     */
   @Override
     public List<Person> getAllPersons(){
        List<Person> list = new ArrayList<Person>();
         list = personMapper.getAllPersons();
        return list;
   }


    /**
     * 导入excel
     * @param file
     * @return
     */
    @Override
    public String readExcelFile(MultipartFile file) {
        String result = "";
        //创建处理EXCEL的类
        ReadExcel readExcel = new ReadExcel();
        //解析excel，获取上传的事件单
        List<Map<String, Object>> userList = readExcel.getExcelInfo(file);
        //至此已经将excel中的数据转换到list里面了,接下来就可以操作list,可以进行保存到数据库,或者其他操作,
        if (userList.size() == 0){
            return "上传失败";
        }
        List<Person> list = new ArrayList<>();
        Person person = null;
        for(Map<String, Object> user:userList){
            person = new Person();
            person.setName(user.get("name").toString());
            person.setAge(Integer.parseInt(user.get("age").toString()));
            person.setPwd(user.get("pwd").toString());
            list.add(person);
            /*int ret = userDao.insertUser(user.get("name").toString(), user.get("sex").toString(), Integer.parseInt(user.get("age").toString()));
            if(ret == 0){
                result = "插入数据库失败";
            }*/
        }
        if (list.size()>0){
            int flage =personMapper.addPersons(list);
            System.out.println("插入数据的个数"+ flage);
            if(flage <1){
                result = "插入数据库失败";
            }
        }
        if(userList != null && !userList.isEmpty()){
            result = "上传成功";
        }else{
            result = "上传失败";
        }
        return result;
    }

    @Override
    public String getUnionId() {
        JSONObject jsonObject = new JSONObject();
        List<Union> list = personMapper.getUnionId();
        jsonObject.put("user_list", list);
        String result = HttpUtil.httpURLConnectionPOST(POSTURL, jsonObject.toJSONString());
        System.out.println(result);
        return result;
    }

    @Override
    public void addPerson(Person person) {
        personMapper.addPerson(person);
    }

    @Override
    public void delPerson(Person person) {
        personMapper.delPerson(person);
    }

    @Override
    public Person findPerson(Person person) {
        return personMapper.findPerson(person);
    }

    @Override
    public void updatePerson(Person person) {
        personMapper.updatePerson(person);
    }

    @Override
    public Integer selAgePerson(Person person) {
        return personMapper.selAgePerson(person);
    }


    @Override
    public Integer seleAgePerson(Person person) {
        return personMapper.seleAgePerson(person);
    }
}
